import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { AppSettings } from '../../shared/config/app-settings';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/operators';
import { CommonResp, Propertylist } from '../interface';
import * as _ from 'lodash';
@Injectable({
  providedIn: 'root'
})
export class ProjectService implements OnInit {

  constructor(
    private httpClient: HttpClient,
  ) {
    // TODO
  }

  ngOnInit(): void {
    // TODO
  }

  getAllPropertyLists(filter?): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    if (filter) {
      header.params = filter;
    }
    return this.httpClient.get<Array<any>>(`${AppSettings.server_base}/propertyList`, header).pipe(map(res => res['success'] ? res['data'] : []));
  }

  getProjectDetail(id): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    return this.httpClient
      .get<Array<any>>(`${AppSettings.server_base}/propertyList/${id}`, header).pipe(
        map((res: CommonResp): Propertylist => res.success ? res.data : {})
      );
  }

  upsertPorpertyListDetail(propertyList): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    return this.httpClient.post(`${AppSettings.server_base}/propertyList`, propertyList, header);
  }

  patch(id, patch): Observable<any>{
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    return this.httpClient.patch<any>(`${AppSettings.server_base}/propertyList/${id}`, patch, header);
  }



  deletePropertyListDetail(id): Observable<any> {
    return this.httpClient
      .delete<Array<any>>(`${AppSettings.mock_base}/shared/propertyList.json?id=${id}`, {
        withCredentials: true,
        params: new HttpParams().set('mock', 'true'),
        headers: new HttpHeaders().set('Content-Type', 'application/json')
      }).pipe(
        map((res: CommonResp): Propertylist => res.success ? res.data : {})
      );
  }

  uploadFile(attachment): Observable<any> {
    const formData: FormData = new FormData();
    let files = attachment.files;
    for (let i = 0; i < files.length; i++) {
      formData.append('file', files[i], files[i].name);
    }
    formData.append("belongToCategory", attachment.belongToCategory);
    formData.append("belongToID", attachment.belongToId);
    return this.httpClient.post(`${AppSettings.server_base}/common/attachment`, formData);
  }

  /**
   * 删除附件，并返回原entity剩余附件列表
   * @param attachment 
   */
  deleteAttachment(attachment) {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    header.params = new HttpParams()
      .set('id', attachment.id)
      .set('belongToID', attachment.belongToID)
      .set('belongToCategory', attachment.belongToCategory);
    return this.httpClient.delete(`${AppSettings.server_base}/common/attachment-return`, header);
  }

  deleteFile(id): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    header.params = new HttpParams().set('id', id);
    return this.httpClient.delete(`${AppSettings.server_base}/common/attachment`, header);
  }

  //-------utils---------


  escapeAll(propertyList, isEscape) {
    if (isEscape) {
      propertyList.buildingOverview = _.escape(propertyList['buildingOverview']);
      propertyList.features = _.escape(propertyList['features']);
    }
    else {
      propertyList.buildingOverview = _.unescape(propertyList['buildingOverview']);
      propertyList.features = _.unescape(propertyList['features']);
    }
  }

  getLogo(item) {
    let logAttachment = item['property-list-logo'] ? item['property-list-logo'][0] : {};
    return logAttachment ? logAttachment['filePath'] : "assets/img/defaultList.png";
  }

  getMainImages(item) {
    let imageAttachment = item['property-list-multi-pictures'] || [];
    let images = [];
    if (imageAttachment)
      _.each(imageAttachment, (image) => images.push(image['filePath']))
    else
      images.push("assets/img/defaultList.png")
    return images;
  }
 
  getFullAddress(project){
    return `${project.buildingAddress?project.buildingAddress+",":""} ${project.city?project.city + ",":""} ${project.state?project.state + ",":""} ${project.country?project.country:""}`
  }

  getShortName(item){
    return item.name;
  }

  getFullName(item) {
    return item.id + " " + item.name + " ," + item.city + "," + item.buildingAddress;
  }


}
